package com.shujia.flink.tf.process;

import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

public class Demo1ProcessFunction {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStream<String> linesDS = env.readTextFile("flink/data/students.csv");

        /*
         * ProcessFunction: 底层的算子
         * 可以代替 map flatmap filter算子
         */
        DataStream<String> clazzDS = linesDS
                .process(new ProcessFunction<String, String>() {
                    /**
                     * processElement方法每一条数据执行一次
                     * @param line： 一行数据
                     * @param ctx： 上下文对象，可以或者flink执行上下文信息,可以或者数据的处理时间
                     * @param out： 用于将数据发送到下游
                     */
                    @Override
                    public void processElement(String line,
                                               ProcessFunction<String, String>.Context ctx,
                                               Collector<String> out) throws Exception {
                        String[] split = line.split(",");
                        String clazz = split[4];
                        String sex = split[3];
                        if ("男".equals(sex)) {
                            //将数据发送到下游
                            out.collect(clazz);
                        }
                    }
                });

        clazzDS.print();
        env.execute();

    }
}
